﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMSearchr
{
    using System.Collections;
    using System.Management;

    public static class DetectionEngine
    {
        public static Hashtable PerformDetection(string server)
        {
            var result = new Hashtable();

            try
            {
                // Create an empty collection
                var servers = new Queue();

                var namespaces = new ManagementClass(new ManagementScope(string.Format(@"\\{0}\root\sms", server)), new ManagementPath("__namespace"), null);
                foreach (var o in namespaces.GetInstances())
                {
                    var ns = (ManagementObject)o;
                    if (ns["Name"].ToString().Contains("site_"))
                    {
                        var wmi = string.Format(@"\\{0}\root\sms\{1}", server, ns["Name"]);
                        var sites = DoWmi(wmi, "SELECT * FROM SMS_Site WHERE");

                        foreach (var site in sites)
                        {
                            result.Add("ServerName", site["ServerName"].ToString());
                            result.Add("SiteCode", site["SiteCode"].ToString());

                            switch (site["Type"].ToString())
                            {
                                case "1":
                                    result.Add("SiteType", "Secondary");
                                    break;

                                case "2":
                                    result.Add("SiteType", "Primary");
                                    break;

                                case "4":
                                    result.Add("SiteType", "CAS");
                                    break;

                                default:
                                    result.Add("SiteType", "Unknown");
                                    break;
                            }

                            result.Add("SiteVersion", site["Version"].ToString());
                        }
                    }
                }

                return result;
            }
            catch (Exception exception)
            {
                return result;
            }
        }

        private static IEnumerable<ManagementObject> DoWmi(string path, string query)
        {
            try
            {
                var managementScope = new ManagementScope();
                var managementPath = new ManagementPath(path);
                managementScope.Path = managementPath;

                // Connect to the namespace
                managementScope.Connect();

                // Prepare query for provider location
                var selectQuery = new SelectQuery(query);

                // Execute query
                using (var objectSearcher = new ManagementObjectSearcher(managementScope, selectQuery))
                {
                    // Return result
                    return objectSearcher.Get().Cast<ManagementObject>();
                }
            }
            catch (Exception exception)
            {
                return new ManagementObjectSearcher().Get().Cast<ManagementObject>();
            }
        }
    }
}
